/****************************************************************************/
/* File: scs-alarm.cpp                                                      */
/* Desc: Implementation file for the core alarm type                        */
/* Created: 2014-11-13                                                      */
/* Revised: 2014-11-13                                                      */
/* Authors: Sterling P. de Mille                                            */
/****************************************************************************/

// specification file
#include "scs-alarm.h"

// constructor
SCSalarm::SCSalarm ()
{
	m_iPaused = SCS_TRUE;
	m_iAlarmEnabled = SCS_FALSE;
	Reset();
}

// destructor
SCSalarm::~SCSalarm ()
{
	m_fDeltaTime = 0;
	m_fElapsedTime = 0;
	m_iPaused = SCS_FALSE;
	m_fAlarmTime = 0;
	m_iAlarmEnabled = SCS_FALSE;
	m_fTimeScale = 0;
}

// update frame time
void SCSalarm::Update ()
{
	if (!m_iPaused) {
		m_fDeltaTime = SCSclock::Singleton()->GetDT();
		m_fElapsedTime += this->GetDT();
	}
}

// update frame time
void SCSalarm::Reset ()
{
	m_fDeltaTime = 0;
	m_fElapsedTime = 0;
	m_fAlarmTime = 0;
	m_fTimeScale = 1;
}

void SCSalarm::Start ()
{
	m_fElapsedTime = 0;
	Resume();
}

void SCSalarm::Restart ()
{
	m_fElapsedTime = 0;
}

void SCSalarm::Stop ()
{
	Pause();
	m_fElapsedTime = 0;
}

void SCSalarm::Pause ()
{
	m_iPaused = SCS_TRUE;
}

void SCSalarm::Resume ()
{
	m_iPaused = SCS_FALSE;
}

void SCSalarm::SetTimeScale (SCSfloat fTimeScale)
{
	m_fTimeScale = fTimeScale;
}

SCSfloat SCSalarm::GetTimeScale ()
{
	return m_fTimeScale;
}

SCSbool SCSalarm::AlarmEnabled ()
{
	return m_iAlarmEnabled;
}

void SCSalarm::EnableAlarm (SCSbool iEnable)
{
	m_iAlarmEnabled = iEnable;
}

SCSbool SCSalarm::TimerDone ()
{
	return (SCSbool)(m_fElapsedTime >= m_fAlarmTime);
}

void SCSalarm::SetAlarmTime (SCSfloat fDays, SCSfloat fHours, SCSfloat fMinutes, SCSfloat fSeconds)
{
	SCSdouble fTotalSeconds = fDays;
	fTotalSeconds *= 24.0;
	fTotalSeconds += fHours;
	fTotalSeconds *= 60.0;
	fTotalSeconds += fMinutes;
	fTotalSeconds *= 60.0;
	fTotalSeconds += fSeconds;

	m_fAlarmTime = fTotalSeconds;
}

void SCSalarm::SetAlarmTime (SCSdouble fSeconds)
{
	m_fAlarmTime = fSeconds;
}

// time per frame
SCSfloat SCSalarm::GetDT ()
{
	return (m_fDeltaTime * m_fTimeScale);
}

// total running time
SCSdouble SCSalarm::GetRemainingTime ()
{
	return (TimerDone() ? 0 : (m_fAlarmTime - m_fElapsedTime));
}

// total running time
SCSdouble SCSalarm::GetElapsedTime ()
{
	return m_fElapsedTime;
}
